package com.shen.JUC.Unsafe;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

public class UnsafeList {
    public static void main(String[] args) {
       /*java.util.ConcurrentModificationException  并发修改异常
        解决并发下ArrayList不安全的问题
        1.new Vector<>()
        2.Collections.synchronizedList(new ArrayList<());
        3.new CopyOnWriteArrayList<>()
        */
        List<String> list = new CopyOnWriteArrayList<>();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0, 5));
                System.out.println(list);
            }, String.valueOf(i)).start();
        }

//        List<String> list = Arrays.asList("1", "2", "3");
//        list.forEach(System.out::println);
    }
}
